/** m2settings is a GUI to configure Maven 2 settings.xml
 *
 *  Copyright (C) 2007 Regis Decamps <decamps@users.sf.net>
 *
 *  This file is part of m2settings.
 *
 *  m2settings is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  or read it online http://www.gnu.org/licenses/gpl.txt
 *
 **/
/*
 * MyProfile.java
 *
 * Created on May 1, 2007, 12:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.decamps.m2settings.beans;

import info.decamps.m2settings.model.Activation;
import info.decamps.m2settings.model.File;
import info.decamps.m2settings.model.Os;
import info.decamps.m2settings.model.PluginRepositories;
import info.decamps.m2settings.model.Profile;
import info.decamps.m2settings.model.Properties;
import info.decamps.m2settings.model.Property;
import info.decamps.m2settings.model.Repositories;
import java.io.BufferedWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.swing.JEditorPane;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author regis
 */
public class MyProfile {
    Profile p;
    
    /** Creates a new instance of MyProfile */
    public MyProfile() {
        p=new Profile();
    }
    
    public MyProfile(Profile profile) {
        p=profile;
    }
    
    public boolean getActivationIsActiveByDefault() {
        return getActivation().getActiveByDefault();
    }
    
    public String getActivationJDK() {
        return getActivation().getJdk();
    }
    
    public String getActivationOSName() {
        return getActivationOs().getName();
    }
    
    public String getActivationOSFamily() {
        return getActivationOs().getFamily();
    }
    
    public String getActivationOSArch() {
        return getActivationOs().getArch();
    }
    
    public String getActivationOSVersion() {
        return getActivationOs().getVersion();
    }
    
    public String getActivationPropertyName() {
        return getActivationProperty().getName();
    }
    
    public String getActivationPropertyValue() {
        return getActivationProperty().getValue();
    }
    
    public String getActivationFileExists() {
        return getActivationFile().getExists();
    }
    
    public String getActivationFileMissing() {
        return getActivationFile().getMissing();
    }
    
    public String getId() {
        return p.getId();
    }
    
    private Activation getActivation() {
        Activation a=p.getActivation();
        if (a==null) {
            a=new Activation();
            p.setActivation(a);
        }
        return a;
    }
    
    private Os getActivationOs() {
        Os os=getActivation().getOs();
        if (os==null) {
            os=new Os();
            getActivation().setOs(os);
        }
        return os;
    }
    
    private Property getActivationProperty() {
        Property p=getActivation().getProperty();
        if (p==null) {
            p=new Property();
            getActivation().setProperty(p);
        }
        return p;
    }
    
    private File getActivationFile() {
        File f=getActivation().getFile();
        if (f==null) {
            f=new File();
            getActivation().setFile(f);
        }
        return f;
    }
    
    public String getProperties() {
        Writer out = new StringWriter();
        try {
            Properties prop=p.getProperties();
            if (prop==null) {
                return "";
            }
            prop.marshal(out);
        } catch (ValidationException ex) {
            ex.printStackTrace();
        } catch (MarshalException ex) {
            ex.printStackTrace();
        }
        return out.toString();
    }
    
    public Repositories getRepositories() {
        return p.getRepositories();
    }
    
    public PluginRepositories getPluginrepositories() {
        return p.getPluginRepositories();
    }
    
    public void setId(String id) {
        p.setId(id);
        
    }
    
    public void setActivationJDK(String jdk) {
        if (StringUtils.isBlank(jdk))
            jdk=null;
        p.getActivation().setJdk(jdk);
    }
    
    public void setActivationFileExists(String f) {
        //TODO p.getActivation().setFile(f);
    }
    
    public void setActivationFileMissing(String f) {
        //TODO p.getActivation().setFile()
    }
    
    public void setActivationProperty(String name, String value) {
        Property prop;
        if (StringUtils.isBlank(name)) {
            prop=null;
        } else {
            prop=new Property();
            prop.setName(name);
            prop.setValue(value);
        }
        p.getActivation().setProperty(prop);
        
    }
    
    public void setActivationOS(String name, String family, String arch, String version) {
        if (StringUtils.isBlank(name)) {
            p.getActivation().setOs(null);
        } else {
            Os os=new Os();
            os.setName(name);
            os.setFamily(family);
            os.setArch(arch);
            os.setVersion(version);
            p.getActivation().setOs(os);
        }
    }
    
    public void setProperties(String xmlString) throws MarshalException, ValidationException {
        if (StringUtils.isBlank(xmlString))         {
            p.setProperties(null);
        } else {
            Reader reader=new StringReader(xmlString);
            Properties prop=(Properties) Properties.unmarshal(reader);
            p.setProperties(prop);
        }
    }
    
    public Profile getProfile() {
        return p;
    }
    
    public void setRepositories(Repositories repositories) {
        p.setRepositories(repositories);
    }
    
    public void setPluginRepositories(PluginRepositories pluginRepositories) {
        p.setPluginRepositories(pluginRepositories);
    }
    
    public void setActivationByDefault(boolean enabled) {
        p.getActivation().setActiveByDefault(enabled);
    }
}